# -*- coding: utf-8 -*-
import pymongo
client = pymongo.MongoClient('47.75.39.50')
client.admin.authenticate("admin321", "dsf::6666,,<<", mechanism='SCRAM-SHA-1')
db = client.city4
data = []
import re
for y in db.stores.find():
    # for yy in y['area']:
        # print yy
        # import ipdb;ipdb.set_trace()
    if y['area']:
        data.append((y['area'][1].encode("utf8"),y['area'][3].encode("utf8")))

data = set(tuple(data))

import MySQLdb
db = MySQLdb.connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8")
cursor = db.cursor()
# sql = "select * from area where tid = 0"
# cursor.execute(sql)
sql = "select name from mode union select name from cate"
cursor.execute(sql)
cateLsit = cursor.fetchall()
# import ipdb;ipdb.set_trace()
names = set()
cateLsit = "|".join([x[0] for x in cateLsit])
for y in data:
    # import ipdb;ipdb.set_trace()
    name = y[1].decode("utf8")
    name = re.sub(cateLsit,"",name)
    if name in names:
        continue
    sql = "select id from area where name = '{}'".format(y[0])
    cursor.execute(sql);
    id = cursor.fetchone()[0]
    sql = "insert into area (name,level,tid) values (%s,%s,%s)"

    ar = (name,2,id)
    cursor.execute(sql,ar);
    names.add(name)
# import ipdb;ipdb.set_trace()
# cursor.executemany(sql,ar)
cursor.close()
db.commit()
